//
//  AddNewAddress_Vm.swift
//  Pods
//
//  Created by eme on 2017/4/1.
//
//
import RxSwift
import UtilCore
import SwiftyJSON
import ModelProtocol
import NetWorkCore

/*
 owner:cy
 update:2017年04月01日11:48:20
 info: 获取地址列表数据
 */
class AddNewAddress_Vm :Base_Vm {
    
    /// 表示正在刷新中
    let loading = ActivityIndicator()
    /*
     接收返回的数据
     */
    let addressModel = PublishSubject<AddressModel>()
    
    ///创建获更新地址信息
    let changeMessage = PublishSubject<String>()
    let addressModelList_Success = PublishSubject<[AddressModel]>()
    
    init(refreshTriger: Observable<String>) {
        super.init()
        refreshTriger
            .map{ AddressApi.changeAddress(addid: $0) }
            .emeRequestApiForObj(AddressModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.addressModel.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        changeMessage
            .map{ AddressApi.changeAddressOrAddNewAddress(message: $0) }
            .emeRequestApiForArray(AddressModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.addressModelList_Success.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        
    }
    
}
